package com.ys.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.ys.common.utils.R;
import com.ys.gulimall.ware.feign.MemberFeignService;
import com.ys.gulimall.ware.vo.FareVo;
import com.ys.gulimall.ware.vo.MemberAddressVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ys.common.utils.PageUtils;
import com.ys.common.utils.Query;

import com.ys.gulimall.ware.dao.WareInfoDao;
import com.ys.gulimall.ware.entity.WareInfoEntity;
import com.ys.gulimall.ware.service.WareInfoService;

import javax.annotation.Resource;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {
	
	@Resource
	private MemberFeignService memberFeignService;
	
	@Override
    public PageUtils queryPage(Map<String, Object> params) {
	    QueryWrapper<WareInfoEntity> queryWrapper = new QueryWrapper<>();
	
	    String key = (String) params.get("key");
	
	    if (!StringUtils.isEmpty(key)) {
		    queryWrapper.eq("id",key)
				    .or().like("name",key)
				    .or().like("address",key)
				    .or().like("areacode",key);
	    }
	
	
	    IPage<WareInfoEntity> page = this.page(
			    new Query<WareInfoEntity>().getPage(params),
			    queryWrapper
	    );
	
	    return new PageUtils(page);
    }
	
	@Override
	public FareVo getFare(Long attrId) {
		
		FareVo fareVo = new FareVo();
		R r = memberFeignService.addrInfo(attrId);
		MemberAddressVo data = r.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {
		});
		if (data != null){
			//模拟计算运费
			String phone = data.getPhone();
			String substring = phone.substring(phone.length() - 1, phone.length());
			BigDecimal bigDecimal = new BigDecimal(substring);
			fareVo.setAddress(data);
			fareVo.setFare(bigDecimal);
			
			return fareVo;
		}
		return null;
	}
	
}